/*
@ auther:ldz
@ time:2018-06-29
*/
//模块介绍
$(function(){
	//服务器地址
	var _httppath = "http://218.246.21.190/";
	//首页数据
	var arry = [];

    //获取token
         $.ajax({
             type: "GET",
			 url: _httppath+"api/appsetting/getToken",
             data: {},
             async:false,
             dataType: "json",
             success: function(response){
     			if (response.status) {
     				//存储token
     				var token =  response.data.access_token;
     				//存储token
					// token = token;	
					//有效期倒计时
					var expires = Number(response.data.expires_in);
					console.log("response",response);	
					var setInterVal = setInterval(function(){
						expires--;
						if (expires == 0) {
							token = "";
							window.clearInterval(setInterVal);
						}
					},1000);

			     if (token != "") {
					//获取图书馆名称信息
			         $.ajax({
			             type: "GET",
						 url: _httppath+"api/appsetting/list"+"?token="+token,
			             data: {},
			             async:false,
			             dataType: "json",
			             success: function(response){
			     			if (response.status) {
			     				var	logoDataArray = response.data;
								console.log("图书馆response-------------",response);
								//设置logo
								  $(".logo_img").attr("src",_httppath+logoDataArray[1].settingValue);
								  // 设置二维码
								  $(".two_code").attr("src",_httppath+logoDataArray[3].settingValue);
			     			}
			             }
			         });
				        




			        //获取菜单列表  method：get 
			         $.ajax({
			             type: "GET",
						 url: _httppath+"api/menu/get"+"?token="+token,
			             data: {},
			             dataType: "json",
		                 async:false,
			             success: function(response){
			     			if (response.status) {
			     				arry = response.data;
			  					console.log("response-------------",response);
			                
								var maxw = $(".swiper-container").css("width").replace("px","");
								var maxh = $(".swiper-container").css("height").replace("px","");
								var ewmin = 132;
								var ehmin = 132;
								var ewmid = 286;
								var ehmid = 132;
								var ewmax = 286;
								var ehmax = 264;
								var _curl = 0;
								var _curt = 0;
								var _ul = $("<ul class = 'swiper-slide'></ul>");
								var parMaxArray = [];
								var _curPageSize = 1;				
								for (let i = 0 ; i < arry.length ; i++) {
				        			let _li	 = $("<li class='s-mod-item show wow'><div class = 'item_box'></div></li>");
				        			let _ob = arry[i];
				        			_li.text(_ob.actionName);
								 	_li.click(function(){
								 		window.location.href = _ob.jumpToAction;
								 	})	

				        			if (_ob.actionType == "min") {        				


										if ((_curl + ewmin) > maxw) {
											_curl = 0;
											_curt += ehmin;
										}

										for (var j = 0; j < parMaxArray.length ; j ++) {
											if (_curt/ehmin == parMaxArray[j].line) {
												if (((_curl + ewmin) > parMaxArray[j].mrl) && (_curl <=  parMaxArray[j].mrl)) {
													_curl = parMaxArray[j].mrl + ewmin;
													if ((_curl + ewmin) > maxw) {
														_curl = 0;
														_curt += ehmin;
													}
												}
											}

										}

					                  	if (i == arry.length - 1) {
						                  	$(".s-mod").append(_ul);
						                 }        				
					        			//换页
					        			if (_curt == maxh && _curl == 0) {
					    					_curl = 0;
					    					_curt = 0;
					    					$(".s-mod").append(_ul);
					    					_ul = $("<ul class = 'swiper-slide'></ul>");
					        			}
				  				
				        				_li.css({
				        					"width":ewmin,
				        					"height":ehmin,
				        					// "backgroundColor":_ob.bgColor,
				        					"left":_curl,
				        					"top":_curt,
				        				})
									 	if (_curl == 0 && _curt != 0) {
									 		_li.addClass('fadeInLeft');
									 	} else if (_curt > 0) {
									 		_li.addClass('fadeInUp');
									 	} else if ((_curl + ewmin) == maxw){
									 		_li.addClass('fadeInRight');
										} else {
									 		_li.addClass('fadeInDown');
									 	}

									 	if (arry[i].name == " ") {
									 		var url = _httppath + arry[i].imgUrl;
									 		_li.find('.item_box').css(
									 			"background-image",'url('+url+')'
									 		)
									 	} else {
									 		var _img = $("<img class='min_icon'>");
									 		var _imgUrl = _httppath + arry[i].imgUrl;
									 		_img.attr("src",_imgUrl);
									 		_li.find('.item_box').css("background",arry[i].bgColor);
									 		_li.find('.item_box').append(_img);

									 		var _span = $("<span class = 'min_title'></span>");
									 		_span.text(arry[i].name);
									 		_li.find('.item_box').append(_span);
									 	}
					
				        				_ul.append(_li);
										_curl += ewmin;	
				        			} else if (_ob.actionType == "mid") {

				 						if ((_curl + ewmid) > maxw) {
											_curl = 0;
											_curt += ehmid;
										}        
										for (var j = 0; j < parMaxArray.length; j ++) {
											if (_curt/ehmid == parMaxArray[j].line) {
												if (((_curl + ewmid) > parMaxArray[j].mrl) && (_curl <=  parMaxArray[j].mrl)) {
													_curl = parMaxArray[j].mrl + ewmid;

						 						if ((_curl + ewmid) > maxw) {
													_curl = 0;
													_curt += ehmid;
												} 
											  }
											}

										}   

					                  	if (i == arry.length - 1) {
						                  	$(".s-mod").append(_ul);
						                  }       				
					        			//换页
					        			if (_curt == maxh && _curl == 0) {
					        				_curl = 0;
					        				_curt = 0;
					    					$(".s-mod").append(_ul);
					    					_ul = $("<ul class = 'swiper-slide'></ul>");
					        			}

				        				_li.css({
				        					"width":ewmid,
				        					"height":ehmid,
				        					// "backgroundColor":_ob.bgColor,
				        					"left":_curl,
				        					"top":_curt,
				        				})
									 	if (_curl == 0 && _curt != 0) {
									 		_li.addClass('fadeInLeft');
									 	} else if (_curt > 0) {
									 		_li.addClass('fadeInUp');
									 	} else if ((_curl + ewmid) == maxw){
									 		_li.addClass('fadeInRight');
										} else {
									 		_li.addClass('fadeInDown');
									 	}

									 	if (arry[i].name == " ") {
									 		var url = _httppath + arry[i].imgUrl;
									 		_li.find('.item_box').css(
									 			"background-image",'url('+url+')'
									 		)
									 	} else {
									 		var _img = $("<img class='mid_icon'>");
									 		var _imgUrl = _httppath + arry[i].imgUrl;
									 		_img.attr("src",_imgUrl);
									 		_li.find('.item_box').css("background",arry[i].bgColor);
									 		_li.find('.item_box').append(_img);

									 		var _span = $("<span class = 'mid_title'></span>");
									 		_span.text(arry[i].name);
									 		_li.find('.item_box').append(_span);
									 	}

					        				_ul.append(_li);  
				        				_curl += ewmid;      				
				        			} else if (_ob.actionType == "max") {
										if ((_curl + ewmax) > maxw) {
											_curl = 0;
											_curt += ehmax;
										}  

										for (var j = 0; j < parMaxArray.length ; j ++) {
											if (_curt/ehmax == parMaxArray[j].line) {
												if (((_curl + ewmax) > parMaxArray[j].mrl) && (_curl <=  parMaxArray[j].mrl)) {
													_curl = parMaxArray[j].mrl + ewmax;

												if ((_curl + ewmax) > maxw) {
													_curl = 0;
													_curt += ehmax;
												} 
												}
											}

										}

				         				var _curline = _curt/ehmax + 1;   
				         				var _obj = {
				         					"line":_curline,
				         					"mrl":_curl,
				         				}
				         				parMaxArray.push(_obj);  	
					                  	if (i == arry.length - 1) {
						                  	$(".s-mod").append(_ul);
						                  }       				
					        			//换页
					        			if (_curt == maxh && _curl == 0) {
					    					$(".s-mod").append(_ul);
					    					_curl = 0;
					    					_curt = 0;
					    					_ul = $("<ul class = 'swiper-slide'></ul>");
					        			}
				        				_li.css({
				        					"width":ewmax,
				        					"height":ehmax,
				        					// "backgroundColor":_ob.bgColor,
				        					"left":_curl,
				        					"top":_curt,       					
				        				})
									 	if (_curl == 0 && _curt != 0) {
									 		_li.addClass('fadeInLeft');
									 	} else if (_curt > 0) {
									 		_li.addClass('fadeInUp');
									 	} else if ((_curl + ewmax) == maxw){
									 		_li.addClass('fadeInRight');
										} else {
									 		_li.addClass('fadeInDown');
									 	}
									 	if (arry[i].name == " ") {
									 		var url = _httppath + arry[i].imgUrl;
									 		_li.find('.item_box').css(
									 			"background-image",'url('+url+')'
									 		)
									 	} else {
									 		var _img = $("<img class='mid_icon'>");
									 		var _imgUrl = _httppath + arry[i].imgUrl;
									 		_img.attr("src",_imgUrl);
									 		_li.find('.item_box').css("background",arry[i].bgColor);
									 		_li.find('.item_box').append(_img);

									 		var _span = $("<span class = 'mid_title'></span>");
									 		_span.text(arry[i].name);
									 		_li.find('.item_box').append(_span);
									 	}
				        				_ul.append(_li);
				         				_curl += ewmax;  			
				        			}

				        		}




							//   <!-- Initialize Swiper -->
						    var swiper = new Swiper('.swiper-container', {
						      pagination: {
						        el: '.swiper-pagination',
						        dynamicBullets: true,
						     },
			    			});


			 				var wow =  new WOW().init();

			      			}
			             }
			         });
			 	   }	
  			}
	 	}
 	 });

})